package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    //获取一级分类的集合
    @GetMapping("/getCategory1")
    public Result getCategory1() {
        List<BaseCategory1> baseCategory1List = managerService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    //获取二级分类的集合
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id) {
        List<BaseCategory2> baseCategory2List = managerService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    //获取三级分类的集合
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id) {
        List<BaseCategory3> baseCategory3List = managerService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    //根据分类id获取平台属性
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = managerService.attrInfoList(category1Id, category2Id, category3Id);
        System.out.println(baseAttrInfoList);
        return Result.ok(baseAttrInfoList);
    }

    //添加或修改平台属性
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        managerService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    //根据平台属性ID获取平台属性
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId) {
        List<BaseAttrValue> baseAttrValueList = managerService.getAttrValueList(attrId);
        return Result.ok(baseAttrValueList);
    }

    //获取品牌分页列表
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result selectPageBaseTrademark(@PathVariable Integer page, @PathVariable Integer limit) {
        IPage<BaseTrademark> Ipage = managerService.selectPageBaseTrademark(page, limit);
        return Result.ok(Ipage);
    }

    //删除品牌
    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result removeBaseTrademark(@PathVariable Long id) {
        managerService.remove(id);
        return Result.ok();
    }

    //添加品牌
    @PostMapping("/baseTrademark/save")
    public Result saveBaseTrademark(@RequestBody BaseTrademark baseTrademark) {
        managerService.save(baseTrademark);
        return Result.ok();
    }

    //根据id获取品牌
    @GetMapping("/baseTrademark/get/{id}")
    public Result getBaseTrademark(@PathVariable Long id) {
        BaseTrademark baseTrademark = managerService.get(id);
        return Result.ok(baseTrademark);
    }

    //修改品牌
    @PutMapping("/baseTrademark/update")
    public Result updateBaseTrademark(@RequestBody BaseTrademark baseTrademark) {
        managerService.update(baseTrademark);
        return Result.ok();
    }

    //查询商品分页 根据三级分类id查
    //请求路径如 http://localhost/admin/product/1/10?category3Id=13 其实第三个参数不加注解也可以
    @GetMapping("/{page}/{limit}")
    public Result spuPage(@PathVariable Integer page, @PathVariable Integer limit, @RequestParam(required = false, defaultValue = "ff", value = "category3Id") Long category3Id) {
        IPage iPage = managerService.spuPage(page, limit, category3Id);
        return Result.ok(iPage);
    }

    //获取品牌(平台)属性
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList() {
        List<BaseTrademark> baseTrademarkList = managerService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    //获取销售属性
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList() {
        List<BaseSaleAttr> baseSaleAttrList = managerService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //添加spu
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        managerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }


    //为了添加SKU 查询spu_image表中spuId对应的所有图片
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId) {
        List<SpuImage> spuImageList = managerService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //为了添加SKU 查询spuId对应的销售属性和属性值集合
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList = managerService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    //添加SKU
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //分页查询SKU
    @GetMapping("/list/{page}/{limit}")
    public Result skuPage(@PathVariable Integer page, @PathVariable Integer limit) {
        IPage iPage = managerService.skuPage(page, limit);
        return Result.ok(iPage);
    }

    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {
        managerService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId) {
        managerService.cancelSale(skuId);
        return Result.ok();
    }
}
